<?php
namespace app\house\model;
use app\BaseModel;
use think\facade\Db;



class AdminArea extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_house_area';
    /**
     * @param $query
     * 查询范围
     */
    public function scopeStatus($query, $status){
        $query->where('status', '>', $status);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房产分类区域
     */
    public function areaList($dis, $level=3){
        $data = $this->where($dis)->status(-1)->order('sort desc')->select()->toArray();
        return $this->getTree($data,0,$level);
    }

    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房产分类区域
     */
    public function areaListV2($dis, $level=3){
        $data = $this->where($dis)->status(-1)->order('sort desc')->select()->toArray();
        return $this->getTreeV2($data,0,$level);
    }
    /**
     * @param $dis
     * @return array
     * @throws \think\exception\DbException
     * 房产分类区域
     */
    public function areaListAdmin($dis, $level=3){
        $data = $this->where($dis)->status(-1)->order('sort desc')->select()->toArray();
        return $this->getTreeAdmin($data,0,$level);
    }
    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     * $level控制反多少级
     */
    public function getTree($data,$pId,$level,$l=-1){
        $tree = array();
        $l++;
        if(!empty($data)&&$l<$level){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTree($data, $v['id'],$level,$l);
//                    if(empty($v['children'])){
//                        unset($v['children']);
//                    }
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }


    public function getTreeV2($data,$pId,$level,$l=-1){
        $tree = array();
        $l++;
        if(!empty($data)&&$l<$level){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTreeV2($data, $v['id'],$level,$l);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }
    /**
     * @param $data
     * @param $pId
     * @return array
     * 递归无限极
     * $level控制反多少级
     */
    public function getTreeAdmin($data,$pId,$level,$l=-1){
        $tree = array();
        $l++;
        if(!empty($data)&&$l<$level){
            foreach($data as $k => $v) {
                if($v['pid'] == $pId) {
                    $v['children'] = $this->getTreeAdmin($data, $v['id'],$level,$l);
                    if(empty($v['children'])){
                        unset($v['children']);
                    }
                    $tree[] = $v;
                }
            }
        }
        return $tree;
    }
    /**
     * @param $id
     * 获取当前level
     *
     */

    public function getLevel($id,&$l=0){
        $l++;
        $data = $this->areaInfo(['id'=>$id]);
        if(!empty($data)&&$l<4){
            $this->getLevel($data['pid'],$l);
        }
        return $l;
    }

    /**
     * @param $id
     * 获取下级id
     */
    public function getLevelId($ids){
        static $level = array();
        $level = array_merge($level,$ids);
        $arr   = $this->where('pid','in',$ids)->column('id');
        if(!empty($arr)){
            $this->getLevelId($arr);
        }
        return $level;
    }


    /**
     * @param $data
     * @return int|string
     * 添加房产区域
     */
    public function areaAdd($data){
        $data['create_time'] = time();
        $data['update_time'] = time();
        $data['status']      = 1;
        $res = $this->insert($data);
        return $res;
    }


    /**
     * @param $dis
     * @param $data
     * @return AdminCate
     * 编辑房产区域
     */
    public function areaUpdate($dis,$data){
        $data['update_time'] = time();
        $res = $this->where($dis)->update($data);
        return $res;

    }
    /**
     * @param $dis
     * 房产区域详情
     */
    public function areaInfo($dis){
        $data = $this->where($dis)->status(-1)->find();
        return !empty($data)?$data->toArray():$data;
    }

    /**
     * User: chenniang
     * Date: 2019-09-18 09:40
     * @param $dis
     * @return mixed
     * descption:获取房产区域pid
     */
    public function areaPid($dis){
        $data = $this->where($dis)->status(-1)->value('pid');
        return $data;
    }


    /**
     * User: chenniang
     * Date: 2019-09-27 19:03
     * @param $dis
     * @return bool
     * descption:删除房产区域
     */
    public function areaDel($dis){
        $res = $this->where($dis)->delete();
        return $res;
    }



}